package com.bappi.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.PreferenceManager;

public class Utils {
	public static final ProgressDialog getProgressDialog(Context context){
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setTitle("Loading");
		progressDialog.setMessage("Please wait...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setCancelable(true);
		progressDialog.setIndeterminate(true);
		return progressDialog;
	}
	public static void showAlertMessage(Context context,String message) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message)
				.setCancelable(false)
				.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {
							public void onClick(final DialogInterface dialog,
									final int id) {
								
							}
						});
		final AlertDialog alert = builder.create();
		alert.show();
	}
	public static final String[] getUserNames(String text){
		String[] ret = null;
		if(text!=null){
			Pattern p = Pattern.compile("@([_a-zA-Z0-9^]*):");
			Matcher m = p.matcher(text);
			List<String> ls = new ArrayList<String>();
			while(m.find()){
				ls.add(m.group(1));
			}
			ret = new String[ls.size()];
			ls.toArray(ret);
		}
		else{
			ret = new String[0];
		}
		return ret;
	}
	public static String[] getLRLS(String text){
		String[] ret = null;
		if(text!=null){
			Pattern p = Pattern.compile("\\b((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
			Matcher m = p.matcher(text);
			List<String> ls = new ArrayList<String>();
			while(m.find()){
				ls.add(m.group(1));
			}
			ret = new String[ls.size()];
			ls.toArray(ret);
		}
		else{
			ret = new String[0];
		}
		return ret;
	}
	
	public static final boolean putValue(Context context, String key,
			String value) {
		return PreferenceManager.getDefaultSharedPreferences(context).edit()
				.putString(key, value).commit();
	}
	public static final String getValue(Context context, String key) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(key, null);
	}
}
